package com.first.group.mainhouse.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphO;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 小刘的小张
 * @version 1.0
 * @description: TODO
 * @date 2021/10/12 19:45
 */
@Controller
@RequestMapping("/mainhouse")
public class IndexController {


    @SentinelResource(value = "main-sentinel", blockHandler = "failMethod")
    @GetMapping({"/", "index.html"})
    public String index() {
        return "index";
    }


//    @GetMapping({"/","index.html"})
//    public String index() {
//        try (Entry entry = SphU.entry("main-sentinel")) { // 被保护的业务逻辑
//            return "index";
//             } catch (BlockException ex) { // 资源访问阻止，被限流或被降级
//            return "fail";
//    }

//    @GetMapping({"/", "index.html"})
//    public String index() {
//        if (SphO.entry("main-sentinel")) { // 务必保证finally会被执行
//            try {
//                return "index";
//            } finally {
//                SphO.exit();
//            }
//        } else { // 资源访问阻止，被限流或被降级 // 进行相应的处理操作
//            return "fail";
//        }
//    }

    public String failMethod(BlockException e) {

        e.printStackTrace();
        return "fail";
    }
}
